package com.project2.service;


import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.project2.bean.CommonData;
import com.project2.bean.ResultVo;
import com.project2.bean.bo.CreatGoodsBo;
import com.project2.bean.bo.GoodsPageBo;
import com.project2.bean.po.*;
import com.project2.bean.vo.Brand;
import com.project2.bean.vo.CatAdnBrand;
import com.project2.bean.vo.Category;
import com.project2.bean.vo.GoodsDetailVo;
import com.project2.mapper.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.List;

@Service
public class AdminGoodsServiceImpl implements AdminGoodsService {

    @Autowired
    MarketGoodsMapper marketGoodsMapper;
    @Autowired
    MarketBrandMapper brandMapper;
    @Autowired
    MarketCategoryMapper categoryMapper;
    @Autowired
    MarketGoodsAttributeMapper attributeMapper;
    @Autowired
    MarketGoodsProductMapper productMapper;
    @Autowired
    MarketGoodsSpecificationMapper specificationMapper;
    @Autowired
    MarketKeywordMapper marketKeywordMapper;

    @Override
    public CommonData<MarketGoods> selectGoods(GoodsPageBo goods) {
        if (goods.getLimit()==0){
            MarketGoodsExample marketGoodsExample = new MarketGoodsExample();
            MarketGoodsExample.Criteria criteria = marketGoodsExample.createCriteria();
            criteria.andDeletedEqualTo(false);
            List<MarketGoods> marketGoods = marketGoodsMapper.selectByExample(marketGoodsExample);
            CommonData<MarketGoods> marketGoodsCommonData = new CommonData<>();
            marketGoodsCommonData.setList(marketGoods);
            marketGoodsCommonData.setTotal(marketGoods.size());
            return marketGoodsCommonData;
        }


        MarketGoodsExample marketGoodsExample = new MarketGoodsExample();
        MarketGoodsExample.Criteria criteria = marketGoodsExample.createCriteria();

        criteria.andDeletedEqualTo(false);


       if (goods.getGoodsId()!=null){
           criteria.andIdEqualTo(goods.getGoodsId());
       }

       if (goods.getGoodsSn()!=null){
           criteria .andGoodsSnEqualTo(String.valueOf(goods.getGoodsSn()));
       }
       if (goods.getName()!=null){
//           criteria.andGoodsSnEqualTo(goods.getName());
          criteria.andNameLike("%" + goods.getName() + "%");
       }

        marketGoodsExample.setOrderByClause(goods.getSort()+" "+goods.getOrder());


        PageHelper.startPage(goods.getPage(),goods.getLimit());

        List<MarketGoods> marketGoods = marketGoodsMapper.selectByExample(marketGoodsExample);

//        CommonData<MarketGoods> marketGoodsCommonData = new CommonData<>();
//        marketGoodsCommonData.setList(marketGoods);

        PageInfo pageInfo = new PageInfo(marketGoods);

        return CommonData.data(pageInfo);
    }



    @Override
    public CatAdnBrand selectCatAndBrand() {
        List<Brand> brandList = brandMapper.selectBrandIdAndName();
        List<Category> categoryList = categoryMapper.selectCategoryIdAndName();
        for (Category category : categoryList) {
            List<Brand> category1 = categoryMapper.selectCategoryByPid(category.getValue());
            category.setChildren(category1);
        }
        CatAdnBrand catAdnBrand = new CatAdnBrand();
        catAdnBrand.setCategoryList(categoryList);
        catAdnBrand.setBrandList(brandList);
//        catAdnBrand.setCategoryList(Collections.singletonList(CategoryList));
//        catAdnBrand.setBrandList(Collections.singletonList(brandList));
        return catAdnBrand;
    }

    @Override
    public ResultVo insertGoods(CreatGoodsBo creatGoods) {
//        MarketGoods goods = creatGoods.getGoods();
//        marketGoodsMapper.insertGoods(goods);


        Date date = new Date();
        MarketGoods goods = creatGoods.getGoods();//商品
        int i = marketGoodsMapper.selectGoods(goods.getName());
        if (i>0){
            return ResultVo.ok("商品已经存在",611);
        }
//        BigDecimal counterPrice = ;
//        BigDecimal pic= BigDecimal.valueOf(20);
        goods.setAddTime(date);
        goods.setUpdateTime(date);
        goods.setDeleted(false);

        BigDecimal bigDecimal = new BigDecimal(20);
        if (creatGoods.getGoods().getCounterPrice()==null){
             return ResultVo.ok("商品金额不能小于0",401);
        }

        if (creatGoods.getGoods().getCounterPrice().compareTo(bigDecimal)==-1){
            goods.setRetailPrice(creatGoods.getGoods().getCounterPrice());
        }else {
            goods.setRetailPrice(goods.getCounterPrice().subtract(bigDecimal));
        }

        int insert = marketGoodsMapper.insert(goods);

        List<MarketGoodsAttribute> attributes = creatGoods.getAttributes();
        for (MarketGoodsAttribute attribute : attributes) {
            attribute.setGoodsId(goods.getId());
            attribute.setAddTime(date);
            attribute.setUpdateTime(date);
            attribute.setDeleted(false);
            int insert2 = attributeMapper.insert(attribute);
        }
        List<MarketGoodsProduct> products = creatGoods.getProducts();
        for (MarketGoodsProduct product : products) {
            product.setGoodsId(goods.getId());
            product.setUrl(goods.getPicUrl());
            product.setAddTime(date);
            product.setId(null);
            product.setUpdateTime(date);
            product.setDeleted(false);
            int insert1 = productMapper.insert(product);
        }

        List<MarketGoodsSpecification> specifications = creatGoods.getSpecifications();
        for (MarketGoodsSpecification specification : specifications) {
            specification.setGoodsId(goods.getId());
            specification.setAddTime(date);
            specification.setUpdateTime(date);
            specification.setDeleted(false);
            int insert1 = specificationMapper.insert(specification);
        }
        //接下来是维护关键字表
        if (goods.getKeywords()!=null){
            MarketKeyword marketKeyword = new MarketKeyword();
            marketKeyword.setAddTime(goods.getAddTime());
            marketKeyword.setDeleted(false);
            marketKeyword.setIsDefault(false);
            marketKeyword.setIsHot(goods.getIsHot());
            marketKeyword.setKeyword(goods.getKeywords());
            marketKeyword.setUpdateTime(goods.getUpdateTime());
            marketKeywordMapper.insertSelective(marketKeyword);
        }
        return ResultVo.ok();
    }


    @Override
    public GoodsDetailVo selectGoodsMsg(Integer id) {
        //查询categoryIds
        int categoryId = marketGoodsMapper.selectCategoryIdByGoodsId(id);
//        int brandId = marketGoodsMapper.selectBrandIdByGoodsId(id);
        int pid = categoryMapper.selectCategoryPid(categoryId);

        ArrayList<Integer> categoryIds = new ArrayList<>();
        categoryIds.add(pid);
        categoryIds.add(categoryId);

        //goods信息
        MarketGoods goods = marketGoodsMapper.selectByPrimaryKey(id);

        //market_goods_attribute属性信息
        List<MarketGoodsAttribute> attributes = attributeMapper.selectAttributeByGoodsId(id);

        //specifications //商品规格
        List<MarketGoodsSpecification> specifications = specificationMapper.selectSpecificationByGoodsId(id);

        //products
        List<MarketGoodsProduct> products = productMapper.selectProductsByGoodsId(id);

        GoodsDetailVo goodsDetailVo = new GoodsDetailVo();
        goodsDetailVo.setGoods(goods);
        goodsDetailVo.setCategoryIds(categoryIds);

        goodsDetailVo.setAttributes(attributes);
        goodsDetailVo.setSpecifications(specifications);
        goodsDetailVo.setProducts(products);


        return goodsDetailVo;
    }

    /*
    TODO 根据商品id进行一系列逻辑删除
   MarketGoodsSpecification
   MarketGoodsProduct
   MarketGoodsAttribute
   MarketGoods
     */
    @Override
    public ResultVo deleteGoods(MarketGoods goods) {
        Date date = new Date();
        Integer goodsId = goods.getId();
        int i = specificationMapper.updateByGoodsId(goodsId, date);
        int i1 = productMapper.updateByGoodsId(goodsId, date);
        int i2 = attributeMapper.updateByGoodsId(goodsId, date);
        int i3 = marketGoodsMapper.updateByGoodsId(goodsId, date);
        return ResultVo.ok("成功",0);
    }



    /*
    TODO 修改商品，先删除再添加
    */
    @Override
    public ResultVo updateGoods(CreatGoodsBo creatGoods) {
        Date date = new Date();
        MarketGoods goods = creatGoods.getGoods();
        List<MarketGoodsAttribute> attributes = creatGoods.getAttributes();
        List<MarketGoodsProduct> products = creatGoods.getProducts();
        List<MarketGoodsSpecification> specifications = creatGoods.getSpecifications();
//
//        MarketGoodsExample goodsExample = new MarketGoodsExample();
//        MarketGoodsAttributeExample attributeExample = new MarketGoodsAttributeExample();
//        MarketGoodsProductExample productExample = new MarketGoodsProductExample();
//        MarketGoodsSpecificationExample specificationExample = new MarketGoodsSpecificationExample();

//        MarketGoodsExample.Criteria criteria = goodsExample.createCriteria();
//        criteria.andIdEqualTo(goods.getId());

        //商品更新
        goods.setUpdateTime(date);
        int i = marketGoodsMapper.updateByPrimaryKeySelective(goods);

        for (MarketGoodsAttribute attribute : attributes) {
            attribute.setUpdateTime(date);
            int i1 = attributeMapper.updateByPrimaryKeySelective(attribute);
        }
        for (MarketGoodsProduct product : products) {
            product.setUpdateTime(date);
            int i1 = productMapper.updateByPrimaryKeySelective(product);
        }
        for (MarketGoodsSpecification specification : specifications) {
            specification.setUpdateTime(date);
            int i1 = specificationMapper.updateByPrimaryKeySelective(specification);
        }

        return ResultVo.ok("成功",0);
    }
}
